package servlet;

import all.JdbcUtils;
import org.apache.commons.lang3.RandomStringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
@WebServlet(urlPatterns = "/goods/insert")
@MultipartConfig(location = "D:\\JAVA\\upload-images",maxFileSize = 100*1024*1024,maxRequestSize = 100*1024*1024)
public class GoodsInsertServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接受参数（文件）
        String name = req.getParameter("n");
        String price = req.getParameter("p");
        Part file = req.getPart("i");
//        String name = file.getSubmittedFileName();
        //2.保存文件
//        file.write("a.png");
        String imaName= RandomStringUtils.randomAlphabetic(10)+System.nanoTime();
        file.write(imaName);
        String sql="INSERT INTO goods(`name`,price,image) VALUES (?,?,?)";
        JdbcUtils.modify(sql,name,price,imaName);
        resp.sendRedirect("/hkp/goods-list.jsp");

    }
}
